from pyx import *
from random import *
from math import *
import os
import sys


def gen_points_in_figures(filename, num_figures=4, avg_points=500, max_x=1000,max_y=1000):
    data_file = open(filename,"w")
    pyx_data_file = open(filename+".pyx_data","w")

    rand = Random()
    rand.seed(rand.random())

    for i in range(num_figures):

        #Choose randomly the figure's center
        f_x = rand.uniform(-max_x,max_x)
        f_y = rand.uniform(-max_y, max_y)

        #Choose the number of points in the figure
        n_points = int( avg_points * rand.uniform(0.75,1.25) ) #+-25%

        #Plot the figure
        figures = [gen_circle, gen_triangle, gen_square]
        #choose a random figure to plot
        fig_fun = rand.choice(figures)
        fig_fun(data_file, pyx_data_file, n_points, max_x, max_y, f_x, f_y)


def gen_circle(data_file, pyx_data_file, n_points, max_x, max_y, c_x, c_y, avg_radious=150):
    rand = Random()
    rand.seed(rand.random())

    #Choose a radious
    c_rad = avg_radious * rand.uniform(0.50,1.50) #+-50%

    #Paint the points randomly, choose a random angle and radius
    for i in range(n_points):
        p_rad = rand.uniform(0,c_rad)
        p_ang = rand.uniform(0,2*pi)

        p_x = p_rad*cos(p_ang) + c_x
        p_y = p_rad*sin(p_ang) + c_y

        if -max_x <= p_x <= max_x and -max_y <= p_y <= max_y :
            data_file.write("%lf,%lf\n"%(p_x,p_y))
            pyx_data_file.write("%lf %lf\n"%(p_x,p_y))

def gen_triangle(data_file, pyx_data_file, n_points, max_x, max_y, t_x, t_y, avg_radious=250):
    rand = Random()
    rand.seed(rand.random())

    #Choose a radious and inclination angle for the triangle
    t_rad = avg_radious * rand.uniform(0.50,1.50) #+-50%
    t_inc = rand.uniform(0,2*pi)

    #Paint the points randomly, choose a random angle and radius
    for i in range(n_points):
        #p_rad = rand.uniform(0,t_rad)
        p_ang = rand.uniform(0,2*pi)

        #calculate the limit for the triangle's border
        alpha = p_ang % (2*pi/3.0)
        beta = 5*pi/6.0 - alpha
        limit = t_rad * sin(pi/6.0) / sin(beta)

        #Calculate the scaled radious
        p_rad = rand.uniform(0,limit)

        p_x = p_rad*cos(p_ang+t_inc) + t_x
        p_y = p_rad*sin(p_ang+t_inc) + t_y

        #Only draw the point if its inside the limits
        #if p_rad <= limit and -max_x <= p_x <= max_x and -max_y <= p_y <= max_y :
        if -max_x <= p_x <= max_x and -max_y <= p_y <= max_y :
            data_file.write("%lf,%lf\n"%(p_x,p_y))
            pyx_data_file.write("%lf %lf\n"%(p_x,p_y))
            
def gen_square(data_file, pyx_data_file, n_points, max_x, max_y, s_x, s_y, avg_side=400):
    rand = Random()
    rand.seed(rand.random())

    #Choose a side size and inclination angle for the triangle
    s_side = avg_side * rand.uniform(0.50,1.50) #+-50%
    s_inc = rand.uniform(0,2*pi)

    #Paint the points randomly, choose a random angle and radius
    for i in range(n_points):
        p_ang = rand.uniform(0,2*pi)

        #calculate the limit for the square's border
        alpha = p_ang % (pi/2.0)
        if alpha > pi/4.0:
            alpha = pi/2.0 - alpha
        beta = pi/2.0 - alpha
        limit = s_side / (2*sin(beta))

        #Calculate the scaled radious
        p_rad = rand.uniform(0,limit)

        p_x = p_rad*cos(p_ang+s_inc) + s_x
        p_y = p_rad*sin(p_ang+s_inc) + s_y

        #Only draw the point if its inside the limits
        #if p_rad <= limit and -max_x <= p_x <= max_x and -max_y <= p_y <= max_y :
        if -max_x <= p_x <= max_x and -max_y <= p_y <= max_y :
            data_file.write("%lf,%lf\n"%(p_x,p_y))
            pyx_data_file.write("%lf %lf\n"%(p_x,p_y))

def plot_points(inputFile,outputFile):
    inputFile+=".pyx_data"
    g = graph.graphxy( width=30, height=30, x=graph.axis.lin(min=-1000,max=1000, title=r"$x$-axis"), y=graph.axis.lin(min=-1000,max=1000, title=r"$y$-axis"))
#g.plot(graph.data.file(inputFile, x=1, y=2), styles=[graph.style.symbol(graph.style.symbol.triangle,symbolattrs=[color.rgb.red])])
    g.plot(graph.data.file(inputFile, x=1, y=2))
    g.writePDFfile(outputFile)

    os.system("rm "+inputFile)

if __name__ == "__main__":
    probName = sys.argv[1]
    numFigures = sys.argv[2]
    gen_points_in_figures(probName+".data", int(numFigures))
    plot_points(probName+".data",probName+".pdf")
